#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Datetime: 2021/10/30 15:09
# @Author  : CHEN Wang
# @Site    :
# @File    : market_index_analysis.py
# @Software: PyCharm

"""
脚本说明: 板块，概念指数分析
"""

import os
import ast
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
from quant_researcher.quant.project_tool.time_tool import get_yesterday, get_today, date_shifter, get_the_end_of_this_month, calc_date_diff
from quant_researcher.quant.datasource_fetch.crypto_api.glassnode import get_prices
from quant_researcher.quant.project_tool.localize import DATA_DIR
from quant_researcher.quant.datasource_fetch.crypto_api.coinmarketcap import get_all_coinlist_via_http
from TQR_Applications.factor_database.crypto.trading_data_weeks import special_max_drawdown_series, get_analysis_coin_list, data_reformat
from quant_researcher.quant.project_tool.file_tool import get_all_filename_path
from quant_researcher.quant.performance_attribution.core_functions.performance_analysis.performance import prices_cleaning

pd.options.mode.chained_assignment = None  # default='warn'


def get_all_data():
    """
    获取剔除稳定币之外的所有币的行情，交易额，市值数据

    :return:
    """

    # 把文件夹中各个小文件中的价格，市值，交易额数据汇总成一个文件
    # 如果之前已经读取过，不用重复读取，直接读取这些数据 all_prices_df, all_amount_df, all_marketcap_df
    file_path = os.path.join(DATA_DIR, 'all_history_ohlcvm_coinmarketcap')
    temp_file_name = os.path.join(file_path, f'all_history_ohlcvm_marketcap')
    history_all_marketcap_df = pd.read_csv(f'{temp_file_name}.csv', index_col='end_date')
    history_date = history_all_marketcap_df.index[-1]
    temp_file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap\all_history_ohlcvm_coinmarketcap')
    temp_file_name = os.path.join(temp_file_path, f'bitcoin')
    history_bitcoin_df = pd.read_excel(f'{temp_file_name}.xlsx', index_col='end_date')
    bitcoin_history_date = history_bitcoin_df.index[-1]
    if calc_date_diff(history_date, bitcoin_history_date) == 0:  # 说明all_prices_df， all_amount_df， all_marketcap_df已经读取汇总并保存过了，无需再保存
        file_name = os.path.join(file_path, f'all_history_ohlcvm_prices_raw')
        all_prices_df = pd.read_csv(f'{file_name}.csv', index_col='end_date')
        file_name = os.path.join(file_path, f'all_history_ohlcvm_amount_raw')
        all_amount_df = pd.read_csv(f'{file_name}.csv', index_col='end_date')
        file_name = os.path.join(file_path, f'all_history_ohlcvm_marketcap_raw')
        all_marketcap_df = pd.read_csv(f'{file_name}.csv', index_col='end_date')
    else:
        print('开始加载全量历史数据-prices, amount, marketcap')
        all_prices_df, all_amount_df, all_marketcap_df = data_reformat()

    all_prices_df = all_prices_df.astype(np.float32)
    print('对价格数据做清洗')
    all_prices_df = all_prices_df.apply(prices_cleaning)
    all_prices_df.dropna(how='all', axis=1, inplace=True)

    # 截取需要分析的时间段,因为可能数据不是同一天获取的，分析截止日设置为最后一期币数大于8000个的日期【如果该日币数少于8000，则不分析】
    start_date = all_prices_df.index[0]
    analysis_end_date = all_prices_df.index[-1]
    coins_num_eachday = all_prices_df.count(axis=1)
    all_days = list(coins_num_eachday.index)
    all_days.sort(reverse=True)
    for day in all_days:
        if coins_num_eachday[day] > 8000:
            analysis_end_date = day
            break
        else:
            continue
    all_prices_df = all_prices_df.loc[start_date:analysis_end_date, ]
    all_amount_df = all_amount_df.loc[start_date:analysis_end_date, ]
    all_marketcap_df = all_marketcap_df.loc[start_date:analysis_end_date, ]

    return all_prices_df, all_amount_df, all_marketcap_df


def index_component():
    end_date = get_today(marker='with_n_dash')

    # 进行板块分析【板块市值加权价格指数，交易额加权价格指数，总市值指数，总交易额，板块币的数量，板块新增币数量，板块周月收益率榜单】
    # 获取所有币的名单
    data = get_all_coinlist_via_http(status='inactive,active,untracked', method='http', tag='all')
    all_coin_list_df = data[0]
    temp_file_path = os.path.join(DATA_DIR, 'all_history_ohlcvm_coinmarketcap/tags_analysis/tag_coin_list')
    file_name = os.path.join(temp_file_path, f'all_coin_list_coinmarketcap_{end_date}')
    data[0].to_excel(f'{file_name}.xlsx')
    all_coin_list_df['tags'] = all_coin_list_df['tags'].astype(str)
    all_tag_series = data[1]
    file_name = os.path.join(temp_file_path, f'all_coin_tag_list_coinmarketcap_{end_date}')
    data[1].to_excel(f'{file_name}.xlsx')

    # 获取每个板块包含哪些币, 并保存
    tag_coin_series = pd.Series(index=all_tag_series, dtype=object)
    for tag in all_tag_series:  # 遍历分析每个板块
        tag_coins = list(all_coin_list_df[all_coin_list_df['tags'].str.contains(tag).fillna(False)]['slug'])
        tag_coin_series[tag] = tag_coins  # 每个板块包含的币
    temp_file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap/tags_analysis/index_components')
    temp_file_name = os.path.join(temp_file_path, f'index_components_{end_date}')
    tag_coin_series.to_excel(f'{temp_file_name}.xlsx')
    temp_file_name = os.path.join(temp_file_path, f'index_components_latest')
    tag_coin_series.to_excel(f'{temp_file_name}.xlsx')


def index_composite():
    today = get_today(marker='with_n_dash')

    # # # 价格数据读取, 不做剔除，也不做最小交易额过滤
    # all_prices_df, all_amount_df, all_marketcap_df = get_all_data()
    # all_ret_df = all_prices_df.pct_change()

    # 数据读取
    file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap')
    temp_file_name = os.path.join(file_path, f'all_history_ohlcvm_daily_ret_cleaned')
    all_ret_df = pd.read_csv(f'{temp_file_name}.csv', index_col='end_date')
    temp_file_name = os.path.join(file_path, f'all_history_ohlcvm_marketcap')
    all_marketcap_df = pd.read_csv(f'{temp_file_name}.csv', index_col='end_date')

    # 跟踪的板块
    selected_tags = ['centralized-exchange',
                     'defi',
                     'decentralized-exchange-dex-token',
                     'lending-borowing',
                     'liquid-staking-derivatives',
                     'memes',
                     'metaverse',
                     'layer-2',
                     'rollups',
                     'zero-knowledge-proofs',
                     'pow', 'pos']

    # 获取板块成份数据
    temp_file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap/tags_analysis/index_components')
    all_filename_path_dict = get_all_filename_path(temp_file_path)
    all_index_components_file = [i.replace('.xlsx', '') for i in all_filename_path_dict.keys()]
    all_index_components_file = [i for i in all_index_components_file if len(i) > 25]
    all_index_components_date = [i[-10:] for i in all_index_components_file]

    # 获取板块成分权重数据（等权，市值加权）
    all_index_components_equal_weight_list = []
    all_index_components_marketcap_weight_list = []
    for i, file in enumerate(all_index_components_file):
        index_components_this_date = pd.read_excel(all_filename_path_dict[f'{file}.xlsx'])
        index_components_this_date.columns = ['tag_name', 'tags']
        if i == 0:
            start_date = '2017-01-01'
        else:
            start_date = all_index_components_date[i]
            start_date = date_shifter(before=start_date, step='days', how_many=1)
            if start_date >= today:
                continue
        if i+1 == len(all_index_components_file):  # 最后一个日期
            end_date = today
        else:
            end_date = all_index_components_date[i+1]
        date_list = pd.date_range(start_date, end_date, freq='D').strftime('%Y-%m-%d')
        for tag in selected_tags:
            equal_weights_df = pd.DataFrame(index=date_list)
            marketcap_weights_df = pd.DataFrame(index=date_list)

            # 等权重
            index_components_list = ast.literal_eval(index_components_this_date[index_components_this_date['tag_name'] == tag]['tags'].values[0])
            index_components_list = [i for i in index_components_list if i in all_ret_df.columns]
            index_components_ret = all_ret_df.loc[date_list[0]:date_list[-1], index_components_list].copy()
            index_components_ret = index_components_ret.where(~ index_components_ret.notnull(), 1)
            equal_weights_df.loc[:, index_components_list] = index_components_ret.div(index_components_ret.sum(axis=1), axis=0)
            equal_weights_df['tag_name'] = tag
            all_index_components_equal_weight_list.append(equal_weights_df)

            date1 = date_shifter(before=date_list[0], step='days', how_many=-1)
            date2 = date_shifter(before=date_list[-1], step='days', how_many=1)
            index_components_list = [i for i in index_components_list if i in all_marketcap_df.columns]
            index_components_marketcap = all_marketcap_df.loc[date1:date2, index_components_list].copy()
            index_components_marketcap = index_components_marketcap.div(index_components_marketcap.sum(axis=1), axis=0)
            index_components_marketcap = index_components_marketcap.shift(1)  # 昨天的市值作为今天的权重计算基础
            marketcap_weights_df.loc[:, index_components_list] = index_components_marketcap.loc[date_list[0]:date_list[-1], :]
            marketcap_weights_df['tag_name'] = tag
            all_index_components_marketcap_weight_list.append(marketcap_weights_df)

    all_index_components_equal_weight_df = pd.concat(all_index_components_equal_weight_list, axis=0)
    all_index_components_marketcap_weight_df = pd.concat(all_index_components_marketcap_weight_list, axis=0)

    # 指数收益率，价格合成
    all_equal_index_ret_list = []
    all_equal_index_price_list = []
    all_marketcap_index_ret_list = []
    all_marketcap_index_price_list = []
    for tag in selected_tags:
        latest_price_data = all_ret_df.index[-1]
        tag_index_components_equal_weight_df = all_index_components_equal_weight_df[all_index_components_equal_weight_df['tag_name'] == tag]
        tag_index_components_equal_weight_df.drop('tag_name', axis=1, inplace=True)
        tag_index_components_equal_weight_df = tag_index_components_equal_weight_df.loc[:latest_price_data, :]
        tag_index_ret = (tag_index_components_equal_weight_df * all_ret_df.loc[tag_index_components_equal_weight_df.index, tag_index_components_equal_weight_df.columns]).sum(axis=1)
        tag_index_ret.name = tag
        all_equal_index_ret_list.append(tag_index_ret)
        tag_index_price = (1 + tag_index_ret).cumprod()
        all_equal_index_price_list.append(tag_index_price)

        tag_index_components_marketcap_weight_df = all_index_components_marketcap_weight_df[all_index_components_marketcap_weight_df['tag_name'] == tag]
        tag_index_components_marketcap_weight_df.drop('tag_name', axis=1, inplace=True)
        tag_index_components_marketcap_weight_df = tag_index_components_marketcap_weight_df.loc[:latest_price_data, :]
        tag_index_ret = (tag_index_components_marketcap_weight_df * all_ret_df.loc[tag_index_components_marketcap_weight_df.index, tag_index_components_marketcap_weight_df.columns]).sum(axis=1)
        tag_index_ret.name = tag
        all_marketcap_index_ret_list.append(tag_index_ret)
        tag_index_price = (1 + tag_index_ret).cumprod()
        all_marketcap_index_price_list.append(tag_index_price)

    temp_file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap/tags_analysis')
    tag_equal_index_ret_df = pd.concat(all_equal_index_ret_list, axis=1)
    temp_file_name = os.path.join(temp_file_path, f'tag_equal_index_ret_df')
    tag_equal_index_ret_df.to_excel(f'{temp_file_name}.xlsx')
    tag_equal_index_price_df = pd.concat(all_equal_index_price_list, axis=1)
    temp_file_name = os.path.join(temp_file_path, f'tag_equal_index_price_df')
    tag_equal_index_price_df.to_excel(f'{temp_file_name}.xlsx')
    tag_marketcap_index_ret_df = pd.concat(all_marketcap_index_ret_list, axis=1)
    temp_file_name = os.path.join(temp_file_path, f'tag_marketcap_index_ret_df')
    tag_marketcap_index_ret_df.to_excel(f'{temp_file_name}.xlsx')
    tag_marketcap_index_price_df = pd.concat(all_marketcap_index_price_list, axis=1)
    temp_file_name = os.path.join(temp_file_path, f'tag_marketcap_index_price_df')
    tag_marketcap_index_price_df.to_excel(f'{temp_file_name}.xlsx')

    # 市值前50， 100， 200成份
    all_equal_index_ret_list = []
    all_equal_index_price_list = []
    all_marketcap_index_ret_list = []
    all_marketcap_index_price_list = []
    for top_num in [5, 10, 20, 50]:
        index_component_equal_weights_df = pd.DataFrame(index=all_ret_df.loc['2017-01-01':, :].index)
        index_component_marketcap_weights_df = pd.DataFrame(index=all_ret_df.loc['2017-01-01':, :].index)
        for date in all_ret_df.loc['2017-01-01':, :].index:
            temp_ret_series = all_ret_df.loc[date, :]
            temp_ret_series.dropna(inplace=True)
            temp_marketcap_series = all_marketcap_df.loc[date, :]
            temp_marketcap_series.dropna(inplace=True)
            temp_coin_list = [i for i in temp_ret_series.index if i in temp_marketcap_series.index]
            temp_marketcap_series = temp_marketcap_series[temp_coin_list]
            temp_marketcap_series.sort_values(ascending=False, inplace=True)
            top_coins = list(temp_marketcap_series[:top_num].index)
            index_component_equal_weights_df.loc[date, top_coins] = 1 / len(top_coins)
            maketcap_weights = temp_marketcap_series[:top_num] / sum(temp_marketcap_series[:top_num])
            index_component_marketcap_weights_df.loc[date, top_coins] = maketcap_weights

        tag_index_ret = (index_component_equal_weights_df * all_ret_df.loc[index_component_equal_weights_df.index, index_component_equal_weights_df.columns]).sum(axis=1)
        tag_index_ret.name = f'topcap_{top_num}'
        all_equal_index_ret_list.append(tag_index_ret)
        tag_index_price = (1 + tag_index_ret).cumprod()
        all_equal_index_price_list.append(tag_index_price)

        tag_index_ret = (index_component_marketcap_weights_df * all_ret_df.loc[index_component_marketcap_weights_df.index, index_component_marketcap_weights_df.columns]).sum(axis=1)
        tag_index_ret.name = f'topcap_{top_num}'
        all_marketcap_index_ret_list.append(tag_index_ret)
        tag_index_price = (1 + tag_index_ret).cumprod()
        all_marketcap_index_price_list.append(tag_index_price)

    temp_file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap/tags_analysis')
    tag_equal_index_ret_df = pd.concat(all_equal_index_ret_list, axis=1)
    temp_file_name = os.path.join(temp_file_path, f'topcap_equal_index_ret_df')
    tag_equal_index_ret_df.to_excel(f'{temp_file_name}.xlsx')
    tag_equal_index_price_df = pd.concat(all_equal_index_price_list, axis=1)
    temp_file_name = os.path.join(temp_file_path, f'topcap_equal_index_price_df')
    tag_equal_index_price_df.to_excel(f'{temp_file_name}.xlsx')
    tag_marketcap_index_ret_df = pd.concat(all_marketcap_index_ret_list, axis=1)
    temp_file_name = os.path.join(temp_file_path, f'topcap_marketcap_index_ret_df')
    tag_marketcap_index_ret_df.to_excel(f'{temp_file_name}.xlsx')
    tag_marketcap_index_price_df = pd.concat(all_marketcap_index_price_list, axis=1)
    temp_file_name = os.path.join(temp_file_path, f'topcap_marketcap_index_price_df')
    tag_marketcap_index_price_df.to_excel(f'{temp_file_name}.xlsx')


def tag_analysis():
    # 数据读取
    file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap')
    temp_file_name = os.path.join(file_path, f'all_history_ohlcvm_daily_ret_cleaned')
    all_ret_df = pd.read_csv(f'{temp_file_name}.csv', index_col='end_date')
    temp_file_name = os.path.join(file_path, f'all_history_ohlcvm_prices')
    all_prices_df = pd.read_csv(f'{temp_file_name}.csv', index_col='end_date')
    temp_file_name = os.path.join(file_path, f'all_history_ohlcvm_amount')
    all_amount_df = pd.read_csv(f'{temp_file_name}.csv', index_col='end_date')
    temp_file_name = os.path.join(file_path, f'all_history_ohlcvm_marketcap')
    all_marketcap_df = pd.read_csv(f'{temp_file_name}.csv', index_col='end_date')

    temp_file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap/tags_analysis/index_components')
    temp_file_name = os.path.join(temp_file_path, f'index_components_latest')
    tag_coin_series = pd.read_excel(f'{temp_file_name}.xlsx')

    # 对需要分析的板块做些筛选
    temp_tag_coin_series = tag_coin_series.copy()
    for tag in tag_coin_series.index:
        # if (len(tag_coin_series[tag]) <= 2):  # 该板块包含的币过少暂不分析
        #     temp_tag_coin_series[tag] = np.NAN
        if ('stablecoin' in tag):  # 稳定币板块不分析
            temp_tag_coin_series[tag] = np.NAN
        if ('portfolio' in tag):  # 某个投资机构的组合不分析
            temp_tag_coin_series[tag] = np.NAN
        # if tag in ['x11', 'x11gost', 'x13', 'x14', 'x15',
        #            'sha-256', 'tokenized-gold', 'tokenized-stock', 'wrapped-tokens']:  # 剔除一些奇怪的板块
        #     temp_tag_coin_series[tag] = np.NAN
    temp_tag_coin_series.dropna(inplace=True)

    tag_cap_weighted_ret_df_list = []
    tag_amount_weighted_ret_df_list = []
    tag_marketcap_df_list = []
    tag_amount_df_list = []
    tag_coin_num_df_list = []

    for tag in temp_tag_coin_series.index:  # 遍历分析每个板块
        tag_coins = tag_coin_series[tag]
        if tag_coins is not []:
            temp_ret_df = all_ret_df[tag_coins]  # 注意这里的ret经过交易额要求过滤
            temp_price_df = all_prices_df[tag_coins]  # 注意这里的price是没有经过交易额要求过滤
            temp_cap_df = all_marketcap_df[tag_coins]
            temp_cap_df = temp_cap_df[~temp_ret_df.isnull()]  # 需要经过ret过滤
            temp_amount_df = all_amount_df[tag_coins]
            temp_amount_df = temp_amount_df[~temp_ret_df.isnull()]  # 需要经过ret过滤

            # 板块市值加权收益率
            tag_cap_weighted_ret_df = (temp_ret_df * temp_cap_df).sum(axis=1) / temp_cap_df.sum(axis=1)
            tag_cap_weighted_ret_df.name = tag
            tag_cap_weighted_ret_df_list.append(tag_cap_weighted_ret_df)
            # 板块交易额加权收益率
            tag_amount_weighted_ret_df = (temp_ret_df * temp_amount_df).sum(axis=1) / temp_amount_df.sum(axis=1)
            tag_amount_weighted_ret_df.name = tag
            tag_amount_weighted_ret_df_list.append(tag_amount_weighted_ret_df)
            # 板块总市值
            tag_marketcap_df = temp_cap_df.sum(axis=1)
            tag_marketcap_df.name = tag
            tag_marketcap_df_list.append(tag_marketcap_df)
            # 板块总交易额
            tag_amount_df = temp_amount_df.sum(axis=1)
            tag_amount_df.name = tag
            tag_amount_df_list.append(tag_amount_df)
            # 板块币总数
            tag_coin_num_df = temp_price_df.count(axis=1)  # 没有做交易额过滤
            tag_coin_num_df.name = tag
            tag_coin_num_df_list.append(tag_coin_num_df)

    all_tag_cap_weighted_ret_df = pd.concat(tag_cap_weighted_ret_df_list, axis=1)
    all_tag_amount_weighted_ret_df = pd.concat(tag_amount_weighted_ret_df_list, axis=1)
    all_tag_marketcap_df = pd.concat(tag_marketcap_df_list, axis=1)
    all_tag_amount_df = pd.concat(tag_amount_df_list, axis=1)
    all_tag_coin_num_df = pd.concat(tag_coin_num_df_list, axis=1)

    latest_date = all_ret_df.index[-1]  # 数据截止日期
    temp_date = datetime.strptime(latest_date, "%Y-%m-%d")
    this_sunday = datetime.strftime(temp_date + timedelta(7 - temp_date.weekday() - 1), "%Y-%m-%d")
    this_month_end = get_the_end_of_this_month(latest_date)
    ### 上周，上月收益率最佳最差十个板块；交易额增速最快降速最快十个板块；发币数量增长最快十个板块
    file_path_temp = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap\tags_analysis')
    import matplotlib.pyplot as plt
    fig = plt.figure()

    # 计算市值加权周收益率，月收益率
    all_tag_cap_weighted_ret_df.index = pd.to_datetime(all_tag_cap_weighted_ret_df.index)
    all_tag_cap_weighted_price_df = (1 + all_tag_cap_weighted_ret_df).cumprod()
    all_tag_cap_weighted_weekly_ret_df = (1 + all_tag_cap_weighted_ret_df).rolling(window=7).apply(np.prod, raw=True) - 1
    all_tag_cap_weighted_weekly_ret_df.sort_values(axis=1, by=latest_date, ascending=False, inplace=True)
    # all_tag_cap_weighted_weekly_ret_df = all_tag_cap_weighted_weekly_ret_df.resample('W').last()
    # all_tag_cap_weighted_weekly_ret_df.sort_values(axis=1, by=this_sunday, ascending=False, inplace=True)
    file_name = os.path.join(file_path_temp, f'best20_cap_weighted_weekly_ret.jpg')
    all_tag_cap_weighted_weekly_ret_df.iloc[-1, :20].plot(kind='barh')
    plt.title(f"{latest_date} best20_cap_weighted_weekly_ret")
    plt.xlabel("weekly ret")
    plt.savefig(f'{file_name}', dpi=1000, bbox_inches='tight')
    fig.clf()
    all_tag_cap_weighted_monthly_ret_df = (1 + all_tag_cap_weighted_ret_df).rolling(window=30).apply(np.prod, raw=True) - 1
    all_tag_cap_weighted_monthly_ret_df.sort_values(axis=1, by=latest_date, ascending=False, inplace=True)
    # all_tag_cap_weighted_monthly_ret_df = all_tag_cap_weighted_monthly_ret_df.resample('M').last()
    # all_tag_cap_weighted_monthly_ret_df.sort_values(axis=1, by=this_month_end, ascending=False, inplace=True)
    file_name = os.path.join(file_path_temp, f'best20_cap_weighted_monthly_ret.jpg')
    all_tag_cap_weighted_monthly_ret_df.iloc[-1, :20].plot(kind='barh')
    plt.title(f"{latest_date} best20_cap_weighted_monthly_ret")
    plt.xlabel("monthly ret")
    plt.savefig(f'{file_name}', dpi=1000, bbox_inches='tight')
    fig.clf()

    # 计算交易额加权周收益率，月收益率
    all_tag_amount_weighted_ret_df.index = pd.to_datetime(all_tag_amount_weighted_ret_df.index)
    all_tag_amount_weighted_price_df = (1 + all_tag_amount_weighted_ret_df).cumprod()
    all_tag_amount_weighted_weekly_ret_df = (1 + all_tag_amount_weighted_ret_df).rolling(window=7).apply(np.prod, raw=True) - 1
    all_tag_amount_weighted_weekly_ret_df.sort_values(axis=1, by=latest_date, ascending=False, inplace=True)
    # all_tag_amount_weighted_weekly_ret_df = all_tag_amount_weighted_weekly_ret_df.resample('W').last()
    # all_tag_amount_weighted_weekly_ret_df.sort_values(axis=1, by=this_sunday, ascending=False, inplace=True)
    file_name = os.path.join(file_path_temp, f'best20_amount_weighted_weekly_ret.jpg')
    all_tag_amount_weighted_weekly_ret_df.iloc[-1, :20].plot(kind='barh')
    plt.title(f"{latest_date} best20_amount_weighted_weekly_ret")
    plt.xlabel("weekly ret")
    plt.savefig(f'{file_name}', dpi=1000, bbox_inches='tight')
    fig.clf()
    all_tag_amount_weighted_monthly_ret_df = (1 + all_tag_amount_weighted_ret_df).rolling(window=30).apply(np.prod, raw=True) - 1
    all_tag_amount_weighted_monthly_ret_df.sort_values(axis=1, by=latest_date, ascending=False, inplace=True)
    # all_tag_amount_weighted_monthly_ret_df = all_tag_amount_weighted_monthly_ret_df.resample('M').last()
    # all_tag_amount_weighted_monthly_ret_df.sort_values(axis=1, by=this_month_end, ascending=False, inplace=True)
    file_name = os.path.join(file_path_temp, f'best20_amount_weighted_monthly_ret.jpg')
    all_tag_amount_weighted_monthly_ret_df.iloc[-1, :20].plot(kind='barh')
    plt.title(f"{latest_date} best20_amount_weighted_monthly_ret")
    plt.xlabel("monthly ret")
    plt.savefig(f'{file_name}', dpi=1000, bbox_inches='tight')
    fig.clf()
    all_tag_amount_weighted_monthly_ret_df['hot_tag_num'] = ((all_tag_amount_weighted_monthly_ret_df - 1) >= 0).sum(axis=1)  # 交易额加权月收益率超过1倍
    all_tag_amount_weighted_monthly_ret_df['tag_num'] = (~ all_tag_amount_weighted_monthly_ret_df.isnull()).sum(axis=1)
    all_tag_amount_weighted_monthly_ret_df['hot_tag_ratio'] = all_tag_amount_weighted_monthly_ret_df['hot_tag_num'] / all_tag_amount_weighted_monthly_ret_df['tag_num']

    # 计算交易额周增速，月增速
    all_tag_amount_df.index = pd.to_datetime(all_tag_amount_df.index)
    all_tag_weekly_amount_df = all_tag_amount_df.rolling(7).sum()  # 统计每天的近七天交易额汇总
    all_tag_amount_weekly_chg_df = all_tag_weekly_amount_df.pct_change(7)  # 计算近7天交易额相对于一周前的近7天的交易额变化率
    all_tag_amount_weekly_chg_df.sort_values(axis=1, by=latest_date, ascending=False, inplace=True)
    # all_tag_amount_weekly_chg_df = all_tag_weekly_amount_df.pct_change(7).resample('W').last()  # 计算近7天交易额相对于一周前的近7天的交易额变化率
    # all_tag_amount_weekly_chg_df.sort_values(axis=1, by=this_sunday, ascending=False, inplace=True)
    file_name = os.path.join(file_path_temp, f'fastest20_weekly_amount_growth.jpg')
    all_tag_amount_weekly_chg_df.iloc[-1, :20].plot(kind='barh')
    plt.title(f"{latest_date} fastest20_weekly_amount_growth")
    plt.xlabel("weekly_amount_growth")
    plt.savefig(f'{file_name}', dpi=1000, bbox_inches='tight')
    fig.clf()
    all_tag_monthly_amount_df = all_tag_amount_df.rolling(30).sum()  # 统计每天的近七天交易额汇总
    all_tag_amount_monthly_chg_df = all_tag_monthly_amount_df.pct_change(30)  # 计算近7天交易额相对于一周前的近7天的交易额变化率
    all_tag_amount_monthly_chg_df.sort_values(axis=1, by=latest_date, ascending=False, inplace=True)
    # all_tag_amount_monthly_chg_df = all_tag_monthly_amount_df.pct_change(30).resample('M').last()  # 计算近7天交易额相对于一周前的近7天的交易额变化率
    # all_tag_amount_monthly_chg_df.sort_values(axis=1, by=this_month_end, ascending=False, inplace=True)
    file_name = os.path.join(file_path_temp, f'fastest20_monthly_amount_growth.jpg')
    all_tag_amount_monthly_chg_df.iloc[-1, :20].plot(kind='barh')
    plt.title(f"{latest_date} fastest20_monthly_amount_growth")
    plt.xlabel("monthly_amount_growth")
    plt.savefig(f'{file_name}', dpi=1000, bbox_inches='tight')
    fig.clf()
    all_tag_amount_monthly_chg_df['hot_tag_num'] = ((all_tag_amount_monthly_chg_df - 2) >= 0).sum(axis=1)  # 月交易额超过2倍
    all_tag_amount_monthly_chg_df['tag_num'] = (~ all_tag_amount_monthly_chg_df.isnull()).sum(axis=1)
    all_tag_amount_monthly_chg_df['hot_tag_ratio'] = all_tag_amount_monthly_chg_df['hot_tag_num'] / all_tag_amount_monthly_chg_df['tag_num']

    # 计算各板块币量周变化，月变化
    all_tag_coin_num_df.index = pd.to_datetime(all_tag_coin_num_df.index)
    all_tag_coin_num_weekly_chg_df = all_tag_coin_num_df.diff(7)  # 近7天币变化量
    all_tag_coin_num_weekly_chg_df.sort_values(axis=1, by=latest_date, ascending=False, inplace=True)
    # all_tag_coin_num_weekly_chg_df = all_tag_coin_num_df.diff(7).resample('W').last()    # 近7天币变化量
    # all_tag_coin_num_weekly_chg_df.sort_values(axis=1, by=this_sunday, ascending=False, inplace=True)
    if (all_tag_coin_num_weekly_chg_df.iloc[-1, :] > 0).sum() > 0:  # 超过多少个板块有新增币
        tag_issue_num = (all_tag_coin_num_weekly_chg_df.iloc[-1, :] > 0).sum()
        file_name = os.path.join(file_path_temp, f'fastest20_weekly_coin_issue.jpg')
        all_tag_coin_num_weekly_chg_df.iloc[-1, :min(tag_issue_num, 10)].plot(kind='barh')
        plt.title(f"{latest_date} fastest20_weekly_coin_issue")
        plt.xlabel("weekly_coin_issue")
        plt.savefig(f'{file_name}', dpi=1000, bbox_inches='tight')
        fig.clf()
    all_tag_coin_num_monthly_chg_df = all_tag_coin_num_df.diff(30)  # 近30天币变化量
    all_tag_coin_num_monthly_chg_df.sort_values(axis=1, by=latest_date, ascending=False, inplace=True)
    # all_tag_coin_num_monthly_chg_df = all_tag_coin_num_df.diff(30).resample('M').last()   # 近30天币变化量
    # all_tag_coin_num_monthly_chg_df.sort_values(axis=1, by=this_month_end, ascending=False, inplace=True)
    if (all_tag_coin_num_monthly_chg_df.iloc[-1, :] > 0).sum() > 0:  # 超过多少个板块有新增币
        tag_issue_num = (all_tag_coin_num_monthly_chg_df.iloc[-1, :] > 0).sum()
        file_name = os.path.join(file_path_temp, f'fastest20_monthly_coin_issue.jpg')
        all_tag_coin_num_monthly_chg_df.iloc[-1, :min(tag_issue_num, 10)].plot(kind='barh')
        plt.title(f"{latest_date} fastest20_monthly_coin_issue")
        plt.xlabel("monthly_coin_issue")
        plt.savefig(f'{file_name}', dpi=1000, bbox_inches='tight')
        fig.clf()
    all_tag_coin_num_monthly_chg_df.replace(0, np.nan, inplace=True)
    all_tag_coin_num_monthly_chg_df['hot_tag_num'] = (all_tag_coin_num_monthly_chg_df >= 3).sum(axis=1)  # 板块币数月增长超3个
    all_tag_coin_num_monthly_chg_df['tag_num'] = (~ all_tag_coin_num_monthly_chg_df.isnull()).sum(axis=1)
    all_tag_coin_num_monthly_chg_df['hot_tag_ratio'] = all_tag_coin_num_monthly_chg_df['hot_tag_num'] / all_tag_coin_num_monthly_chg_df['tag_num']

    plt.close()

    for index, df_list in enumerate([[all_tag_cap_weighted_price_df, all_tag_cap_weighted_ret_df, all_tag_cap_weighted_weekly_ret_df, all_tag_cap_weighted_monthly_ret_df],
                                     [all_tag_amount_weighted_price_df, all_tag_amount_weighted_ret_df, all_tag_amount_weighted_weekly_ret_df, all_tag_amount_weighted_monthly_ret_df],
                                     [all_tag_marketcap_df],
                                     [all_tag_amount_df, all_tag_amount_weekly_chg_df, all_tag_amount_monthly_chg_df],
                                     [all_tag_coin_num_df, all_tag_coin_num_weekly_chg_df, all_tag_coin_num_monthly_chg_df],
                                     [tag_coin_series]]):
        if index == 0:
            df_name = ['cap_weighted_price', 'cap_weighted_ret', 'cap_weighted_weekly_ret', 'cap_weighted_monthly_ret']
        elif index == 1:
            df_name = ['amount_weighted_price', 'amount_weighted_ret', 'amount_weighted_weekly_ret', 'amount_weighted_monthly_ret']
        elif index == 2:
            df_name = ['marketcap']
        elif index == 3:
            df_name = ['amount', 'amount_weekly_chg', 'amount_monthly_chg']
        elif index == 4:
            df_name = ['coin_num', 'coin_num_weekly_chg', 'coin_num_monthly_chg']
        elif index == 5:
            df_name = ['coin_name']

        for i, each_df in enumerate(df_list):
            file_name = os.path.join(file_path_temp, f'All Tags {df_name[i]}')
            each_df.to_excel(f'{file_name}.xlsx')


if __name__ == '__main__':
    # # 指数成分
    # index_component()

    # 指数合成
    index_composite()

    # # 板块分析
    # tag_analysis()